﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Location
{
    public class Vara_Data_Site_Controller : Vara_Data_ISite_Actions
    {
        private static List<Vara_Data_ISite> _Sites;
        public List<Vara_Data_ISite> Sites
        {
            get
            {
                if (_Sites == null)
                    Refresh_Site_List();
                return _Sites;
            }
        }

        public void Refresh_Site_List()
        {
            DataTable DataTableSites;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableSites = NewSqlConnector.ExecuteProcedure("Return_Locations");
            }
            List<Vara_Data_ISite> ListOfSites = new List<Vara_Data_ISite>();
            foreach (DataRow R in DataTableSites.Rows)
            {
                Vara_Data_ISite SiteToAppend = new Vara_Data_Site(int.Parse(R["ID"].ToString()));
                SiteToAppend.Name = R["Name"].ToString();
                SiteToAppend.Address1 = R["Address1"].ToString();
                SiteToAppend.Address2 = R["Address2"].ToString();
                SiteToAppend.Address3 = R["Address3"].ToString();
                SiteToAppend.Postcode = R["PostCode"].ToString();
                SiteToAppend.IsInternal = Convert.ToBoolean(R["InternalLocation"].ToString());
                ListOfSites.Add(SiteToAppend);
            }
            _Sites = ListOfSites;
        }

        public Vara_Data_ISite ReturnSite(int ID)
        {
            Vara_Data_ISite SiteToReturn;
            SiteToReturn = Sites.Find(delegate(Vara_Data_ISite Site) { return Site.ID == ID; });
            return SiteToReturn;
        }
    }
}
